$vcffile = "SNV_FINAL_TARGET.vcf.gz";
chomp($vcffile);
system("rm $vcffile");
@array=('1','2','3','4','5','7','8','9','10','11','12','13','14','16','17','18','19','20','22');
for($i=0;$i<@array;$i++)
{
	$inputfile = "SNV.chr$array[$i].vcf.gz";
	chomp($inputfile);
	if(-e $inputfile)
	{
		open(BUFF,"gunzip -c $inputfile |") or die "gunzip input file doesn't exists\n";
	}
	else
	{
		die "gunzip input file $inputfile doesn't exists\n"
	}
	if (-e $vcffile) 
	{
		open(WRBUFF,"| /projects/bsi/bictools/apps/alignment/tabix/current/bgzip >> $vcffile") or die "not able to append to $vcffile\n";
		$num=0;
		while($line = <BUFF>)
                {
			 if($line =~ m/^#CHROM/)
                         {
                         	if($prev ne $line)
				{
					die "not equal\n $prev  $line\n";
				}
                         }
			if($line !~ m/^#/)
			{
                        	print WRBUFF $line;
				$num++;
			}
                }
		print "chromosome$array[$i] $num\n"; 
	} 
	else
	{
		open(WRBUFF,"| /projects/bsi/bictools/apps/alignment/tabix/current/bgzip >$vcffile") or die "not able to write to $vcffile\n";
		$num=0;
		while($line = <BUFF>)
		{
			if($line !~ m/^##contig/)
			{
				if($line =~ m/^#CHROM/)
				{
					$prev=$line;
				}
				print WRBUFF $line;
				$num++;
			}
		}
		print "chromosome 1 $num\n";
	}
}
